//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package SaveServlet.course;

import com.lyq.bean.Course;
import com.lyq.bean.service.E_Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/MyCourse_Servlet"})
public class MyCourse_Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public MyCourse_Servlet() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String campus = request.getParameter("campus");
        String semester = request.getParameter("semester");
        
        if (id == null) {
            id = "";
        }
        
        if (campus == null) {
            campus = "";
        }
        
        if (semester == null || semester.isEmpty()) {
            semester = "2025-2026秋季学期"; // 默认学期
        }
        
        if (id.equals("seekall")) {
            String sNo = (String) request.getSession().getAttribute("UNAME");
            List<Course> list = new ArrayList<Course>();
            list = (new E_Service()).seek_ElectC(sNo);
            
            // 根据校区和学期筛选课程
            list = this.filterByCampusAndSemester(list, campus, semester);
            
            request.setAttribute("list", list);
            request.getRequestDispatcher("/jsp/course/s/MyCourse.jsp?campus=" + campus + "&semester=" + semester).forward(request, response);
        } else if (id.equals("recede")) {
            String cNo = request.getParameter("No");
            String sNo = (String) request.getSession().getAttribute("UNAME");
            String message = "";
            Boolean result = (new E_Service()).recede_C(cNo, sNo);
            if (result) {
                message = "退课成功！";
            } else {
                message = "退课失败，请稍后重试！";
            }
            
            request.getSession().setAttribute("message", message);
            request.getSession().setAttribute("url", "MyCourse_Servlet?id=seekall&campus=" + campus + "&semester=" + semester);
            response.sendRedirect("Message.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // POST方法暂时保持不变
        doGet(request, response);
    }
    
    // 根据校区和学期筛选课程
    private List<Course> filterByCampusAndSemester(List<Course> courses, String campus, String semester) {
        List<Course> filteredCourses = new ArrayList<Course>();
        for (Course course : courses) {
            boolean campusMatch = true;
            boolean semesterMatch = true;
            
            // 校区筛选
            if (campus != null && !campus.isEmpty()) {
                campusMatch = (course.getCampus() != null && course.getCampus().equals(campus));
            }
            
            // 学期筛选
            if (semester != null && !semester.isEmpty()) {
                semesterMatch = (course.getSemester() != null && course.getSemester().equals(semester));
            }
            
            // 只有当校区和学期都匹配时才添加到结果中
            if (campusMatch && semesterMatch) {
                filteredCourses.add(course);
            }
        }
        return filteredCourses;
    }
}